<template>
  <div v-loading="loading">
    <el-input
      v-model="seqNo"
      :placeholder="placeholder"
      :readonly="readonly"
      :disabled="disabled"
    >
      <el-button
        slot="append"
        :disabled="seqNo!==''"
        @click="createSeqNo">生成</el-button>
    </el-input>
  </div>
</template>

<script>
/**
 * @author yizuomin
 * @date 2023/5/7 15:32
 **/
import { generateSeqNo } from '@/api/system/sequenceRule'
export default {
  name: 'SequenceInput',
  props: {
    seqKey: {
      type: String,
      default() {
        return 'BH'
      }
    },
    value: {
      type: String,
      default() {
        return ''
      }
    },
    placeholder: {
      type: String,
      default() {
        return '请生成编号'
      }
    },
    disabled: {
      type: Boolean,
      default() {
        return false
      }
    },
    readonly: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      seqNo: '',
      loading: false
    }
  },
  watch: {
    value(v1, v2) {
      this.initValue(v1)
    }
  },
  methods: {
    createSeqNo() {
      this.loading = true
      generateSeqNo({ key: this.seqKey }).then(resp => {
        if (resp.success) {
          this.seqNo = resp.data
        }
      }).finally(() => {
        this.loading = false
        this.$emit('change', this.seqNo)
      })
    },
    initValue(val) {
      this.seqNo = val
    }
  }
}
</script>

<style lang="scss" scoped>

</style>

